package net.alpcontrol.bi.colligation.service.impl;

import cn.hutool.core.util.ArrayUtil;
import net.alpcontrol.bi.colligation.entity.PjProjectBuilding;
import net.alpcontrol.bi.colligation.entity.po.ProjectBuildingPo;
import net.alpcontrol.bi.colligation.entity.po.ProjectBuildingSeriesPo;
import net.alpcontrol.bi.colligation.mapper.PjProjectBuildingMapper;
import net.alpcontrol.bi.colligation.service.IPjProjectBuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丛庆
 * @since 2020-09-11
 */
@Service
@Transactional
public class PjProjectBuildingServiceImpl extends ServiceImpl<PjProjectBuildingMapper, PjProjectBuilding> implements IPjProjectBuildingService {

    @Autowired
    private PjProjectBuildingMapper pjProjectBuildingMapper;

    /**
      *  分页查询
      * @param bootStrapTable
      * @param pjProjectBuilding
      * @return
      */
    @Override
    public IPage<PjProjectBuilding> selectPage(Page<PjProjectBuilding> page) {
        return pjProjectBuildingMapper.selectPage(page,null);
    }

    @Override
    public List<ProjectBuildingPo> getByName(String projectName) {
        // 1.根据项目名称查询该项目所有状态记录
        List<PjProjectBuilding> projectBuildings = pjProjectBuildingMapper.getByProjectName(projectName);
        // 2.封装甘特图所需数据
        List<ProjectBuildingPo> result = transform(projectBuildings);

        return result;
    }

    @Override
    public List<ProjectBuildingPo> getByXmbm(String xmbm) {
        // 1.根据项目名称查询该项目所有状态记录
        List<PjProjectBuilding> projectBuildings = pjProjectBuildingMapper.getByXmbm(xmbm);
        // 2.封装甘特图所需数据
        List<ProjectBuildingPo> result = transform(projectBuildings);

        return result;
    }

    @Override
    public Map getKpiByXmbm(String xmbm) {
        Map res = new HashMap();
        res.put("statusYwts",pjProjectBuildingMapper.getByXmbmGroupByStatus(xmbm));
        res.put("sjgq",pjProjectBuildingMapper.getSjgqByXmbm(xmbm));
        return res;
    }

    private List<ProjectBuildingPo> transform(List<PjProjectBuilding> projectBuildings){
        List<ProjectBuildingPo> result = new ArrayList<>(projectBuildings.size());
        int i = 1;
        for(PjProjectBuilding pjProjectBuilding : projectBuildings){
            ProjectBuildingPo projectBuildingPo = new ProjectBuildingPo();
            projectBuildingPo.setId(i++);
            projectBuildingPo.setName(pjProjectBuilding.getMxaSgjdhdmc());

            ProjectBuildingSeriesPo plan = new ProjectBuildingSeriesPo();
            plan.setName("计划");
            plan.setStart(pjProjectBuilding.getMxbJhkssj().getTime());
            plan.setEnd(pjProjectBuilding.getMxbJhwcsj().getTime());

            ProjectBuildingSeriesPo actual = new ProjectBuildingSeriesPo();
            actual.setName("实际");
            actual.setStart(pjProjectBuilding.getMxaSjkssj().getTime());
            actual.setEnd(pjProjectBuilding.getMxaSjwcsj().getTime());

            if(pjProjectBuilding.getMxaSjwcsj().getTime() > pjProjectBuilding.getMxbJhwcsj().getTime()){
                actual.setColor("#DC143C");
            } else {
                actual.setColor("#32CD32");
            }


            List<ProjectBuildingSeriesPo> series = new ArrayList<>();
            series.add(plan);
            series.add(actual);
            projectBuildingPo.setSeries(series);

            result.add(projectBuildingPo);
        }
        return result;
    }
}
